/*
 * CS_CREATE_GUILD.cpp
 *
 *  Created on: 2014. 7. 31.
 *      Author: spencer
 */




#include "../../User.h"
#include "../../Command.h"
#include "../../Proxy.h"
#include "../../NState/Static.h"

#include "../protocol/Guild.h"

namespace ProjectZ { namespace NCommand {

void Command::CS_CREATE_GUILD(User * pUser, ::Network::Packet * pPacket)
{
/*
CS_GUILD_CREATE								0x3260
{
	u8				u8Is_recruit;				//길드원 모집 여부 //0: 모집중 아님, 1:모집중   2:모집중 암호필요
	str				strGuild_password;			//길드 패스워드 (  4자리 자연수, 존재하지 않으면 : NULL   )
	str				strGuild_name;				//길드명
	str				strGuild_message;			//길드 모집글
}

SC_GUILD_CREATE								0x3261
{
	s16				s16ack;								/// ACK_OK: 성공  ,
																	/// ACK_GUILD_ALREADY_EXIST: 존재함-사용불가,
																	/// ACK_GUILD_PERMISSION_ERROR : 생성권한없음.
																	/// ACK_GUILD_TIME_ERROR : 길드탈퇴후 24시간이 지나지 않음.
																	/// ACK_NOT_ENOUGH_GOLD : 골드 부족
	u32				u32guild_seq;						// 생성된 길드번호

}
*/
	PREPARE_PACKET;

	uint8_t is_Recruit = 0;
	Toolkit::Str<4> strPassword;
	Toolkit::Str<18> strName;
	Toolkit::Str<48> strNoticeText;

	*pPacket >> is_Recruit >> strPassword >> strName >> strNoticeText;

	if (0 > is_Recruit || is_Recruit > 1)
	{
		::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
		*pAck << static_cast<int16_t>(ACK_UNKNOWN_ERROR);
		pAck->MakePacket(CMD_SC_GUILD_CREATE);
		pUser->Send(pAck);
		return;
	}

//	DEBUGLOG("%s> packet> name: %s, password: %s, notice_text: %s, exposure: %d"
//			, __FUNCTION__
//			, strName.c_str(), strPassword.c_str(), strNoticeText.c_str(), is_Recruit
//			);


	if (0 < pUser->GetUserInfo().guild_seq)
	{
		DEBUGLOG("%s> FAILED> ACK_GUILD_CREATE_JOINED_ALREADY", __FUNCTION__ );

		::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
		*pAck << static_cast<int16_t>(ACK_GUILD_CREATE_JOINED_ALREADY);
		pAck->MakePacket(CMD_SC_GUILD_CREATE);
		pUser->Send(pAck);
		return;
	}


	CreateGuildSyn syn;
	syn.user_seq	= pUser->GetUserSeq();
	syn.gold = pUser->GetCharacterInfo().gold;
	syn.level = static_cast<int16_t>(pUser->GetCharacterInfo().level);
	syn.guild_name = strName.c_str();
	syn.password = strPassword.c_str();
	syn.notice_text = strNoticeText.c_str();
	syn.exposure = static_cast<int16_t>(is_Recruit);
	syn.server_id = static_cast<int16_t>(ProjectZ::Config::Instance().GetResourceDatabase()._channelUniqueIndex);
	//char *stop;
	//syn.socialid = strtoul(pUser->GetSocialID().c_str(), &stop, 10)
	syn.socialid = atoll(pUser->GetSocialID().c_str());
	syn.nickname = pUser->GetUserNickName().c_str();
	syn.profile_url = pUser->GetUserInfo().profile_url.c_str();


//	DEBUGLOG("%s> syn 10> user_seq: %d, gold: %d, level: %d, name: %s, password: %s, notice_text: %s, exposure: %d"
//			, __FUNCTION__
//			, syn.user_seq, syn.gold, syn.level, syn.guild_name.c_str(), syn.password.c_str(), syn.notice_text.c_str(), syn.exposure
//			);

//	DEBUGLOG("%s> syn 20> server_id: %d, socialid: %u, nickname: %s"
//			, __FUNCTION__
//			, syn.server_id, syn.socialid, syn.nickname.c_str()
//			);

	//DEBUGLOG("%s> syn 20> ", __FUNCTION__ );

	if (false == NProxy::SendGuildServer(pUser, syn))
	{
		//DEBUGLOG("%s> FAILED> 29", __FUNCTION__ );

		::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
		*pAck << static_cast<int16_t>(ACK_GUILD_CREATE_FAILED);
		pAck->MakePacket(CMD_SC_GUILD_CREATE);
		pUser->Send(pAck);
		return;
	}

	//DEBUGLOG("%s> syn 30> fin", __FUNCTION__ );

	pUser->SetState(NState::Static::Instance().WAITGUILDINFO());
}

} /*NCommand*/ } /*ProjectZ*/
